<template>
  <div class="login-container">
    <vue-danmaku
      ref="danmaku"
      v-model:danmus="danmus"
      :right="50"
      :top="25"
      :channels="3"
      :debounce="1200"
      :randomChannel="true"
      useSlot
      loop
      style="height: 200px; width: 100%"
    >
      <template v-slot:dm="{ danmu }">
        <span class="danmu-span" :style="{ color: danmu.color }">
          {{ danmu.text }}
        </span>
      </template>
    </vue-danmaku>
    <el-form ref="formRef" :model="form" class="login-form" :rules="rules">
      <div class="title-container">
        <h3 class="title">{{ $t('login.title') }}</h3>
      </div>
      <el-form-item prop="username">
        <svg-icon icon="user" class="svg-container"></svg-icon>
        <el-input v-model="form.username" autocomplete="username" />
      </el-form-item>

      <el-form-item prop="password">
        <svg-icon icon="password" class="svg-container"></svg-icon>
        <el-input
          v-model="form.password"
          :type="passwordType"
          autocomplete="current-password"
        />
        <svg-icon
          :icon="passwordType === 'password' ? 'eye' : 'eye-open'"
          @click="changeType"
        ></svg-icon>
      </el-form-item>

      <el-button type="primary" class="login-button" @click="handleLogin">{{
        $t('login.btnTitle')
      }}</el-button>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import vueDanmaku from 'vue3-danmaku'
import { useStore } from 'vuex'
const store = useStore()

const danmus = ref([
  { text: '腾讯弹幕来袭', color: '#DDA71EFF' },
  { text: '🐧弹幕来捏', color: '#E92121FF' },
  { text: '爱奇艺弹幕来啦', color: '#E92121FF' },
  { text: '爱奇艺来啦', color: '#FFFFFFFF' },
  { text: 'B友来了', color: '#2ACAE7FF' },
  { text: '哔哩哔哩来了', color: '#663399FF' },
  { text: '第一', color: '#FFFFFFFF' },
  { text: '第一', color: '#DDA71EFF' },
  { text: '前排前排', color: '#79D2DEFF' },
  { text: '前排前排', color: '#663399FF' },
  { text: '🐱优酷88会员留言', color: '#663399FF' },
  { text: '优酷88会员留言', color: '#E92121FF' }
])

const rules = ref({
  username: [
    {
      required: true,
      message: 'place input username',
      trigger: 'blur'
    }
  ],
  password: [
    {
      required: true,
      message: 'place input password',
      trigger: 'blur'
    }
  ]
})

const passwordType = ref('password')
const changeType = () => {
  if (passwordType.value === 'password') {
    passwordType.value = 'text'
  } else {
    passwordType.value = 'password'
  }
}

const formRef = ref(null)

const handleLogin = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      store.dispatch('app/login', form.value)
    } else {
      return false
    }
  })
}

const form = ref({
  username: '',
  password: ''
})
</script>

<style lang="scss" scoped>
.danmu-span {
  border-width: 10px;
  font-size: 24px;
  border-color: rgb(30, 174, 184);
  font-weight: bolder;
  font-family: 'PingFang';
}

$bg: #2d3a4b;
$dark_gray: #bdccd4;
$light_gray: #eee;
$cursor: #fff;

.login-container {
  height: 100dvh;
  width: 100dvw;
  background: linear-gradient(90deg, #a4a0c7 0, #60b8b7 58dvw);
  overflow: hidden;

  .login-form {
    display: flex;
    flex-direction: column;
    position: relative;
    width: 70dvw;
    max-width: 100vmin;
    padding: 0px 35px 0;
    margin: 0 auto;
    overflow: hidden;

    ::v-deep .el-form-item {
      width: 100dvw;
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }

    ::v-deep .el-input {
      display: inline-block;
      height: 47px;
      width: 60dvw;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;
      }
    }
    .login-button {
      width: 100%;
      box-sizing: border-box;
    }
  }

  .tips {
    font-size: 16px;
    line-height: 28px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }

    ::v-deep .lang-select {
      width: 100%;
      position: absolute;
      top: 4px;
      right: 0;
      background-color: white;
      font-size: 22px;
      padding: 4px;
      border-radius: 4px;
      cursor: pointer;
    }
  }

  .show-pwd {
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

::v-deep .el-input__wrapper {
  box-shadow: none;
  background-color: transparent;
  display: inline-block;
  width: 100%;
}

::v-deep input:-webkit-autofill {
  background-color: #ffffff !important;
  transition: background-color 5000s ease-in-out 0s !important;
}
</style>
